package com.guaoran.controller;

import com.guaoran.jpa.entity.VpgMovie;
import com.guaoran.jpa.entity.VpgPackage;
import com.guaoran.mybatis.bean.Demo;
import com.guaoran.service.DemoService;
import com.guaoran.utils.ResultVOUtil;
import com.guaoran.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : 孤傲然
 * @Description :
 * @date :2018/1/25 13:37
 */
@Slf4j
@RestController
@RequestMapping("/json")
public class DemoJsonController {
    @Autowired
    private DemoService demoService;

    /**
     * 查询所有demo
     * @return
     */
    @GetMapping("/findAllDemos")
    @Cacheable(cacheNames = "findAllDemos",key = "'findAllDemos'")
    public List<Demo> findAllDemos(){
        List<Demo> demoList = demoService.findAllDemos();
        return demoList;
    }

    /**
     * 通过名称查询
     * @param name
     * @return
     */
    @GetMapping("/queryDemosByName")
    @Cacheable(cacheNames = "queryDemosByName",key = "#name" ,condition = "#name.length()>1")//根据参数做缓存,如果参数长度大于2，则做缓存，否则不做缓存
//    @Cacheable(cacheNames = "queryDemosByName",key = "#name",unless = "#result.code!=0")//根据参数做缓存，如果返回结果的code是0，则缓存，否则不缓存
    public ResultVO<Demo> queryDemosByName(@RequestParam (value = "name") String name){
        log.info("name={}",name);
        List<Demo> demoList = demoService.queryDemosByName(name);
        return ResultVOUtil.success(demoList);
    }

    /**
     * 通过供应商查询影片包
     * @param providerId
     * @return
     * @throws Exception
     */
    @GetMapping("/findVPgPackageByProviderId")
    public List<VpgPackage> findVPgPackageByProviderId(String providerId) throws Exception{
        Pageable pageable = new PageRequest(0,20);
        Page<VpgPackage> vpgPackagePage = demoService.findAllByProviderId(providerId, pageable);
        List<VpgPackage> vpgPackageList = vpgPackagePage.getContent();
        return vpgPackageList;
    }

    /**
     * 查询
     * @param packageId
     * @return
     */
    @GetMapping("/findPackageById")
    @Cacheable(cacheNames = "findPackageById",key = "#packageId")//设置缓存
    public VpgPackage findPackageById(@RequestParam(value = "packageId") String packageId){
        log.info("packageId={}",packageId);
        VpgPackage vpgPackage = demoService.queryPackageById(Long.parseLong(packageId));
        return vpgPackage;
    }

    /**
     * 保存影片信息
     * @param assetId
     * @param assetName
     * @param providerId
     * @return
     */
    @PostMapping("/saveVpgPackage")
    public VpgPackage saveVpgPackage(String assetId,String assetName,String providerId){
        VpgPackage vpgPackage = new VpgPackage(null,assetId,assetName,providerId);
        demoService.saveVpgPackage(vpgPackage);
        return vpgPackage;
    }
    /**
     * 修改影片信息
     * @param assetId
     * @param assetName
     * @param providerId
     * @param id
     * @return
     */
    @PostMapping("/updateVpgPackage")
    @CachePut(cacheNames = "findPackageById",key = "#id")//修改后更新缓存
    public VpgPackage updateVpgPackage(String assetId,String assetName,String providerId,String id){
        log.info("assetId={},assetName={},providerId={},id={}",assetId,assetName,providerId,id);
        Long packageId = null;
        if(!StringUtils.isEmpty(id)){
            packageId = Long.parseLong(id);
        }
        VpgPackage vpgPackage = new VpgPackage(packageId,assetId,assetName,providerId);
        demoService.saveVpgPackage(vpgPackage);
        return vpgPackage;
    }

    /**
     * 保存影片
     * @param title
     * @return
     */
    @PostMapping("/saveMovie")
    public VpgMovie saveMovie(String title){
        log.info("title={}",title);
        VpgMovie vpgMovie = new VpgMovie
                (2L,"macp"+ com.guaoran.utils.StringUtils.getRandomNum(),
                title,"成龙，李连杰，阿米尔汗，杰森斯坦森","葛优","2018",1L);
        demoService.saveVpgMovie(vpgMovie);
        return vpgMovie;
    }
}
